﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using _XNA__RunningDeath.GLO;
using _XNA__RunningDeath.GUI;
using _XNA__RunningDeath.DAO;
using Microsoft.Xna.Framework.Storage;


namespace _XNA__RunningDeath
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        #region contractor
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        CGUI_MenuMain _guiMenuMain;
        CGUI_About _guiAbout;
        CGUI_Help _guiHelp;
        CGUI_HighScore _guiHighScore;
        CGUI_LoadGame _guiLoadGame;
        CGUI_Pause _guiPause;
        CGUI_SaveGame _guiSave;
        CGUI_SelectMap _guiSelectMap;
        CGUI_Setting _guiSetting;
        CGUI_RunningDeath _guiRunningDeath;

        #endregion

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferHeight = 576;
            graphics.PreferredBackBufferWidth = 1024;
            
            this.Window.Title = "GAME CD";
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            this.IsMouseVisible = true;
            base.Initialize();
            this._guiMenuMain = new CGUI_MenuMain(Content, null, spriteBatch);
            this._guiAbout = new CGUI_About(Content, null, spriteBatch);
            this._guiHelp = new CGUI_Help(Content, null, spriteBatch);
            this._guiHighScore = new CGUI_HighScore(Content,null,spriteBatch);
            this._guiLoadGame = new CGUI_LoadGame(Content,null,spriteBatch);
            this._guiPause = new CGUI_Pause(Content, null, spriteBatch);
            this._guiSave = new CGUI_SaveGame(Content, null, spriteBatch);
            this._guiSelectMap = new CGUI_SelectMap(Content, null, spriteBatch);
            this._guiRunningDeath = new CGUI_RunningDeath(Content, null, spriteBatch);
            this._guiSetting = new CGUI_Setting(Content, null, spriteBatch);
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            CGLOBAL._nhacnen = Content.Load<Song>(@"AmThanh/songNen");
            CGLOBAL.soundFXbtn = Content.Load<SoundEffect>(@"AmThanh/btnclick");
            MediaPlayer.Play(CGLOBAL._nhacnen);

            /*data.Life = 3;
            data.PlayerName = "nameKimDuyen";
            data.Score = 100;*/
            
            //Save game
           // data =  SaveGame.doLoad("kimDuyen");
            
        }

        protected override void UnloadContent()
        {
            
        }
        
        protected override void Update(GameTime gameTime)
        {
           
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            if(CGLOBAL.GUISelected==GUIenum.exit)
            {
                this.Exit();
            }

            MediaPlayer.Volume = CThamSo.volumMusic;
            EventButton(gameTime);
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            //spriteBatch.Begin();
            //this._guiMenuMain.Draw(gameTime);

            switch (CGLOBAL.GUISelected)
            {
                case GUIenum.guiAbout:
                    this._guiAbout.Draw(gameTime);
                    break;
                case GUIenum.guiMainMenu:
                    this._guiMenuMain.Draw(gameTime);
                    break;
                case GUIenum.guiHelp:
                    this._guiHelp.Draw(gameTime);
                    break;
                case GUIenum.guiHighScore:
                    this._guiHighScore.Draw(gameTime);
                    break;
                case GUIenum.guiLoadGame:
                    this._guiLoadGame.Draw(gameTime);
                    break;
                case GUIenum.guiPause:
                    this._guiPause.Draw(gameTime);
                    break;
                case GUIenum.guiRunningDeath:
                    this._guiRunningDeath.Draw(gameTime);
                    break;
                case GUIenum.guiSaveGame:
                    this._guiSave.Draw(gameTime);
                    break;
                case GUIenum.guiSelectMap:
                    this._guiSelectMap.Draw(gameTime);
                    break;
                case GUIenum.guiSetting:
                    this._guiSetting.Draw(gameTime);
                    break;
            }

            base.Draw(gameTime);
        }

        /// <summary>
        /// Xử lý các sự kiện kích vào các nút có trong giao diên
        /// </summary>
        /// <param name="gameTime"></param>
        private void EventButton(GameTime gameTime)
        {
            switch (CGLOBAL.GUISelected)
            {
                case GUIenum.guiAbout:
                    if (CGLOBAL.NextGUI)
                    {
                        this._guiAbout = new CGUI_About(Content, null, spriteBatch);
                        CGLOBAL.NextGUI = false;
                    }
                    this._guiAbout.Update(gameTime);
                    break;
                case GUIenum.guiSetting:
                    if (CGLOBAL.NextGUI)
                    {
                        this._guiSetting = new CGUI_Setting(Content, null, spriteBatch);
                        CGLOBAL.NextGUI = false;
                    }
                    this._guiSetting.Update(gameTime);
                    break;
                case GUIenum.guiHelp:
                    if (CGLOBAL.NextGUI)
                    {
                        this._guiHelp = new CGUI_Help(Content, null, spriteBatch);
                        CGLOBAL.NextGUI = false;
                    }
                    this._guiHelp.Update(gameTime);
                    break;
                case GUIenum.guiHighScore:
                    if (CGLOBAL.NextGUI)
                    {
                        this._guiHighScore = new CGUI_HighScore(Content, null, spriteBatch);
                        CGLOBAL.NextGUI = false;
                    }
                    this._guiHighScore.Update(gameTime);
                    break;
                case GUIenum.guiLoadGame:
                    if (CGLOBAL.NextGUI)
                    {
                        this._guiLoadGame = new CGUI_LoadGame(Content, null, spriteBatch);
                        CGLOBAL.NextGUI = false;
                    }
                    this._guiLoadGame.Update(gameTime);
                    break;
                case GUIenum.guiPause:
                    if (CGLOBAL.NextGUI)
                    {
                        this._guiPause = new CGUI_Pause(Content, null, spriteBatch);
                        CGLOBAL.NextGUI = false;
                    }
                    this._guiPause.Update(gameTime);
                    break;
                case GUIenum.guiRunningDeath:
                    if (CGLOBAL.NextGUI)
                    {
                        this._guiRunningDeath = new CGUI_RunningDeath(Content, null, spriteBatch);
                        CGLOBAL.NextGUI = false;
                    }
                    this._guiRunningDeath.Update(gameTime);
                    break;
                case GUIenum.guiSaveGame:
                    if (CGLOBAL.NextGUI)
                    {
                        this._guiSave = new CGUI_SaveGame(Content, null, spriteBatch);
                        CGLOBAL.NextGUI = false;
                    }
                    this._guiSave.Update(gameTime);
                    break;
                case GUIenum.guiSelectMap:
                    if (CGLOBAL.NextGUI)
                    {
                        this._guiSelectMap = new CGUI_SelectMap(Content, null, spriteBatch);
                        CGLOBAL.NextGUI = false;
                    }
                    this._guiSelectMap.Update(gameTime);
                    break;
                case GUIenum.guiMainMenu:
                    this._guiMenuMain.Update(gameTime);
                    break;
            }
        }
    }
}
